﻿using System.Linq;
using System.Collections.Generic;
using System;
using MongoDB.Driver;
using Sustainalytics.MongoDB.Extensions;

using System;
using System.Collections.Generic;
using System.Linq;
using System;
using System.Collections.Generic;
using System.Linq;

using System.Threading;
using System.Threading.Tasks;

namespace DockerPlay
{
    internal class Program
    {
        private static int recordsInserted = 1;
        private static readonly IMongoCollection<ContainerStatus> ContainerStatusCollection =
            MongoFactory.GetCollectionFromDatabase<ContainerStatus>(ConnectionStrings.ClientPlatform);

        private static readonly IMongoCollection<CorporateDataBasic> CorporateDataBasicSyncCollection = 
            MongoFactory.GetCollectionFromDatabase<CorporateDataBasic>(ConnectionStrings.ClientDWSync);

        private static readonly IMongoCollection<CorporateDataBasic> CorporateDataBasicAppCollection =
            MongoFactory.GetCollectionFromDatabase<CorporateDataBasic>(ConnectionStrings.ClientDW);

        private static readonly DockerHelper DockerHelper = new DockerHelper(ConnectionStrings.DockerMachineGA);
        private static readonly DockerOrchestrator DockerOrchestrator = new DockerOrchestrator(DockerHelper);

        private static void Main(string[] args)
        {
            Console.WriteLine("Waiting for new item to be inserted");
            Console.WriteLine("Press ESC to stop");

            while (!Console.KeyAvailable)
            {
                StartSync().Wait();
                Console.WriteLine($"Inserted record {recordsInserted++}");
                Console.ReadKey();
            }
        }

        private static async Task StartSync()
        {
            var activeContainer = await DockerHelper.GetActiveContainer();
            Console.WriteLine("Clone current app database in progress ...");
            await DockerOrchestrator.StartSyncContainer(activeContainer);
            Console.WriteLine("Sync database started");

            var corpDataFromSync = new CorporateDataBasic()
            {
                Id = Guid.NewGuid(),
                CompanyName = $"From sync {recordsInserted}",
                MainExchange = "Exchange",
                Date = DateTime.Now
            };
            CorporateDataBasicSyncCollection.InsertOne(corpDataFromSync);
            Console.WriteLine($"Inserted new {nameof(CorporateDataBasic)} with id {corpDataFromSync.Id} in sync database");
            Thread.Sleep(1000);

            var newActiveContainer = await DockerOrchestrator.SwitchActiveContainer(activeContainer);

            Console.WriteLine("Add sync database as application database");

            var containersAfterEndSync = await DockerHelper.GetContainersIdAndStatus();

            ContainerStatusCollection.InsertOne(containersAfterEndSync.First(x => x.Name == newActiveContainer));
            Console.WriteLine($"Inserted active container in {nameof(ContainerStatus)} collection, Client Platform Database");
        }

        public class CorporateDataBasic
        {
            public Guid Id { get; set; }
            public string CompanyName { get; set; }
            public string CompanyNameLowercase { get; set; }
            public string CompanyNameUrlFriendly { get; set; }

            public string MainExchange { get; set; }
            public string MainTicker { get; set; }

            public List<int> UniverseIds { get; set; }
            public Guid? ResearchParentId { get; set; }
            public DateTime Date { get; set; }
            //public List<Guid> CoverageIds { get; set; }
            //public List<Guid> SiblingsIds { get; set; }
        }
    }
}
